package cn.jisol.ngame.actions.state2d;

import cn.jisol.ngame.actions.state2d.entity.NSyncWorldConfig;
import cn.jisol.ngame.actions.statesync.client.NStateSyncClient;
import cn.jisol.ngame.ncall.NCallServiceImpl;
import cn.jisol.ngame.rpc.NGameRPCClass;
import cn.jisol.ngame.rpc.NGameRPCMethod;
import cn.jisol.ngame.rpc.NRPCParam;
import cn.jisol.ngame.sync.bridge.NSyncBridgeMode;
import cn.jisol.ngame.sync.state.NStateWorldStep;
import cn.jisol.ngame.sync.state.NSyncStateMode;
import com.alibaba.fastjson.JSON;
import lombok.Getter;
import lombok.Setter;
import org.dyn4j.dynamics.Body;
import org.springframework.stereotype.Component;

import java.util.Iterator;
import java.util.Objects;

@Getter
@Setter
@Component
@NGameRPCClass
public class SNState2DServer extends NCallServiceImpl {

    public NSyncStateMode world = null;

    /**
     * 初始化世界
     */
    public void nGameSyncStart(){

        if(Objects.nonNull(this.world)) return;

        //初始化世界
        this.world = new NSyncStateMode();

        addRegister(this.world);
        this.world.start();

        System.out.println("SNState2DServer - nGameSyncStart : 启动2D世界");

    }

    @NStateWorldStep
    public void nStateWorldStep(NSyncStateMode world){
        world.getAllBody().forEach((nSyncBody) -> {
            System.out.println(nSyncBody.uuid+":"+nSyncBody.body.getTransform().toString());
        });
    }

    //添加Body
    public void addBody(Body body){
        this.world.addBody(body);
    }

    @NGameRPCMethod
    public void nAddWorldInfo(@NRPCParam("data") NSyncWorldConfig data){

        System.out.println(data);
        this.world.config(data);

    }

}
